# coding=utf-8
import cgi
import webapp2
from google.appengine.ext import db
from util.my_jinja2_env import Jinja2_my_env


class Topics(db.Model):
    t_id = db.IntegerProperty()
    t_topic = db.StringProperty()
    t_content = db.StringProperty(multiline=True)
    t_url = db.StringProperty()
    t_date = db.DateTimeProperty(auto_now_add=True)


class PostPage(webapp2.RequestHandler):
    t_id = 0
    def get(self):
        template_values={}
        template = Jinja2_my_env().getEnv().get_template(Jinja2_my_env.HTML_PATH + 'post.html')
        self.response.write(template.render(template_values)) 
        
        
    def post(self):
        try:
            str_topic = self.request.get('topic')
            content = self.request.get('content')
            topic = Topics(t_url="https://www.google.com")
            topic.t_id = (self.t_id+1)
            topic.t_topic = str_topic
            topic.t_content = cgi.escape(content)
            topic.put()
            
            self.redirect('/post')
        except Exception as e:
            self.response.write(e)
        
        
